<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="昵称" prop="username">
      <el-input v-model="dataForm.username" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="header">
      <!-- <el-input v-model="dataForm.header" placeholder=""></el-input> -->
      <single-upload v-model="dataForm.header"></single-upload>
    </el-form-item>
    <!-- <el-form-item label="gender" prop="gender">
      <el-input v-model="dataForm.gender" placeholder=""></el-input>
    </el-form-item> -->
    <el-form-item label="性别  " prop="gender">
      <el-select v-model="dataForm.gender">
        <el-option label="男" value="男"></el-option>
        <el-option label="女" value="女"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="出生日期" prop="birth">
      <!-- <el-input v-model="dataForm.birth" placeholder=""></el-input> -->
      <el-date-picker v-model="dataForm.birth" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
    </el-form-item>
    <el-form-item label="积分" prop="integration">
      <el-input v-model="dataForm.integration" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from '@/components/upload/singleUpload'
export default {
  components: { SingleUpload },
  data () {
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的邮箱'))
    }
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^[1][0-9]{10}$/
      if (regMobile.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return {
      visible: false,
      dataForm: {
        id: 0,
        username: '',
        password: '',
        mobile: '',
        email: '',
        header: '',
        gender: '',
        birth: '',
        integration: '',
        status: '',
        createTime: ''
      },
      dataRule: {
        username: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        header: [
          // { required: true, message: '不能为空', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        birth: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getCurrentTime () {
      // 获取当前时间
      var _this = this
      let yy = new Date().getFullYear()
      let mm = new Date().getMonth() + 1
      let dd = new Date().getDate()
      let hh = new Date().getHours()
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
      return _this.gettime
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/user/user/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.username = data.user.username
              this.dataForm.password = data.user.password
              this.dataForm.mobile = data.user.mobile
              this.dataForm.email = data.user.email
              this.dataForm.header = data.user.header
              this.dataForm.gender = data.user.gender
              this.dataForm.birth = data.user.birth
              this.dataForm.integration = data.user.integration
              this.dataForm.status = data.user.status
              let temp = parseInt(data.user.createTime.slice(11, 13)) + 8
              this.dataForm.createTime = data.user.createTime.slice(0, 10) + ' ' + temp + data.user.createTime.slice(13, 16)
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/user/user/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'username': this.dataForm.username,
              'password': this.dataForm.password,
              'mobile': this.dataForm.mobile,
              'email': this.dataForm.email,
              'header': this.dataForm.header,
              'gender': this.dataForm.gender,
              'birth': this.dataForm.birth,
              'integration': this.dataForm.integration,
              'status': this.dataForm.status,
              'createTime': !this.dataForm.id ? this.getCurrentTime : this.dataForm.createTime.slice(0, 10) + 'T' + this.dataForm.createTime.slice(11, 16)
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
